﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MySocketHelper
{
    /// <summary>
    /// 核心,实现Socket监听,收发信息等操作.
    /// SocketManager m_socket = new SocketManager(200, 1024);  
    /// m_socket.Init();  
    /// m_socket.Start(new IPEndPoint(IPAddress.Any, 13909));  
    /// </summary>
    public class SocketManager
    {
        private int m_maxConnectNum;    //最大连接数  
        private int m_revBufferSize;    //最大接收字节数  
        BufferManager m_bufferManager;  
        const int opsToAlloc = 2;  
        Socket listenSocket;            //监听Socket  
        SocketEventPool m_pool;  
        int m_clientCount;              //连接的客户端数量  
        Semaphore m_maxNumberAcceptedClients;  
  
        List<AsyncUserToken> m_clients; //客户端列表  
 
        #region 定义委托  
  
        /// <summary>  
        /// 客户端连接数量变化时触发  
        /// </summary>  
        /// <param name="num">当前增加客户的个数(用户退出时为负数,增加时为正数,一般为1)</param>  
        /// <param name="token">增加用户的信息</param>  
        public delegate void OnClientNumberChange(int num, AsyncUserToken token);  
  
        /// <summary>  
        /// 接收到客户端的数据  
        /// </summary>  
        /// <param name="token">客户端</param>  
        /// <param name="buff">客户端数据</param>  
        public delegate void OnReceiveData(AsyncUserToken token, byte[] buff);  
 
        #endregion  
 
        #region 定义事件  
        /// <summary>  
        /// 客户端连接数量变化事件  
        /// </summary>  
        public event OnClientNumberChange ClientNumberChange;  
  
        /// <summary>  
        /// 接收到客户端的数据事件  
        /// </summary>  
        public event OnReceiveData ReceiveClientData;  
 
 
        #endregion  
 
        #region 定义属性  
  
        /// <summary>  
        /// 获取客户端列表  
        /// </summary>  
        public List<AsyncUserToken> ClientList { get { return m_clients; } }  
 
        #endregion  
  
        /// <summary>  
        /// 构造函数  
        /// </summary>  
        /// <param name="numConnections">最大连接数</param>  
        /// <param name="receiveBufferSize">缓存区大小</param>  
        public SocketManager(int numConnections, int receiveBufferSize)  
        {  
            m_clientCount = 0;  //连接客户端数量
            m_maxConnectNum = numConnections;  //允许最大连接数
            m_revBufferSize = receiveBufferSize;  //缓存区大小
            // allocate buffers such that the maximum number of sockets can have one outstanding read and   
            //write posted to the socket simultaneously    
            m_bufferManager = new BufferManager(receiveBufferSize * numConnections * opsToAlloc, receiveBufferSize);  
  
            m_pool = new SocketEventPool(numConnections);  
            m_maxNumberAcceptedClients = new Semaphore(numConnections, numConnections);  
        }  
  
        /// <summary>  
        /// 初始化  
        /// </summary>  
        public void Init()  
        {  
            // Allocates one large byte buffer which all I/O operations use a piece of.  This gaurds   
            // against memory fragmentation  
            m_bufferManager.InitBuffer();  
            m_clients = new List<AsyncUserToken>();  
            // preallocate pool of SocketAsyncEventArgs objects  
            SocketAsyncEventArgs readWriteEventArg;  
  
            for (int i = 0; i < m_maxConnectNum; i++)  
            {  
                readWriteEventArg = new SocketAsyncEventArgs();  
                readWriteEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);  
                readWriteEventArg.UserToken = new AsyncUserToken();  
  
                // assign a byte buffer from the buffer pool to the SocketAsyncEventArg object  
                m_bufferManager.SetBuffer(readWriteEventArg);  
                // add SocketAsyncEventArg to the pool  
                m_pool.Push(readWriteEventArg);  
            }  
        }  
  
  
        /// <summary>  
        /// 启动服务  
        /// </summary>  
        /// <param name="localEndPoint"></param>  
        public bool Start(IPEndPoint localEndPoint)  
        {  
            try  
            {  
                m_clients.Clear();  
                listenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);  
                listenSocket.Bind(localEndPoint);  
                // start the server with a listen backlog of 100 connections  
                listenSocket.Listen(m_maxConnectNum);  
                // post accepts on the listening socket  
                StartAccept(null);  
                return true;  
            }  
            catch (Exception)  
            {  
                return false;  
            }  
        }  
  
        /// <summary>  
        /// 停止服务  
        /// </summary>  
        public void Stop()  
        {
            //foreach (AsyncUserToken token in m_clients)
            //{
            //    try
            //    {
            //        token.Socket.Shutdown(SocketShutdown.Both);
            //    }
            //    catch (Exception ex) 
            //    { 
            //        //throw new Exception("服务停止异常！"+ex.Message); 
            //        Common.ErrorRecord.WriteErrorLog("服务停止异常!" + ex.Message);
            //    }
            //}
            for (int i = 0; i < m_clients.Count; i++)
            {
                try
                {
                    m_clients[i].Socket.Shutdown(SocketShutdown.Both);
                }
                catch (Exception ex)
                {
                    //throw new Exception("服务停止异常！"+ex.Message); 
                    Common.ErrorRecord.WriteErrorLog("服务停止异常!" + ex.Message);
                }
            }
                //try
                //{
                //    listenSocket.Shutdown(SocketShutdown.Both);
                //}
                //catch (Exception) { }  
                if (listenSocket != null)
                    listenSocket.Close();  
            int c_count = m_clients.Count;  
            lock (m_clients) { m_clients.Clear(); }  
  
            if (ClientNumberChange != null)  
                ClientNumberChange(-c_count, null);  
        }  
  
  
        public void CloseClient(AsyncUserToken token)  
        {  
            try  
            {  
                token.Socket.Shutdown(SocketShutdown.Both);  
            }
            catch (Exception ex) 
            {
                Common.ErrorRecord.WriteErrorLog("客户端关闭异常！异常信息：" + ex.Message);
                throw new Exception("客户端关闭异常！异常信息：" + ex.Message); 
            }  
        }  
  
  
        // Begins an operation to accept a connection request from the client   
        //  
        // <param name="acceptEventArg">The context object to use when issuing   
        // the accept operation on the server's listening socket</param>  
        public void StartAccept(SocketAsyncEventArgs acceptEventArg)  
        {  
            if (acceptEventArg == null)  
            {  
                acceptEventArg = new SocketAsyncEventArgs();  
                acceptEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(AcceptEventArg_Completed);  
            }  
            else  
            {  
                // socket must be cleared since the context object is being reused  
                acceptEventArg.AcceptSocket = null;  
            }  
  
            m_maxNumberAcceptedClients.WaitOne();  
            if (!listenSocket.AcceptAsync(acceptEventArg))  
            {  
                ProcessAccept(acceptEventArg);  
            }  
        }  
  
        // This method is the callback method associated with Socket.AcceptAsync   
        // operations and is invoked when an accept operation is complete  
        //  
        void AcceptEventArg_Completed(object sender, SocketAsyncEventArgs e)  
        {  
            ProcessAccept(e);  
        }  
  
        private void ProcessAccept(SocketAsyncEventArgs e)  
        {
            try
            {  
                Interlocked.Increment(ref m_clientCount);  
                // Get the socket for the accepted client connection and put it into the   
                //ReadEventArg object user token  
                SocketAsyncEventArgs readEventArgs = m_pool.Pop();  
                AsyncUserToken userToken = (AsyncUserToken)readEventArgs.UserToken;
                if (e.AcceptSocket != null)
                {
                    userToken.Socket = e.AcceptSocket;
                    userToken.ConnectTime = DateTime.Now;
                    if (e.AcceptSocket.RemoteEndPoint != null)
                    {
                        userToken.Remote = e.AcceptSocket.RemoteEndPoint;
                        userToken.IPAddress = ((IPEndPoint)(e.AcceptSocket.RemoteEndPoint)).Address;  
                    }                   
                }

                lock (m_clients)
                {
                    //如果该连接已经存在则先清除之前的连接
                    int index = m_clients.FindIndex(s => userToken.IPAddress.Equals(s.IPAddress));
                    if (index != -1)
                    {
                        CloseClient(m_clients[index]);
                        //m_clients.RemoveAt(index);
                    }
                    m_clients.Add(userToken);
                }   
  
                if (ClientNumberChange != null)  
                    ClientNumberChange(1, userToken);  
                if (!e.AcceptSocket.ReceiveAsync(readEventArgs))  
                {  
                    ProcessReceive(readEventArgs);  
                }
            }
            catch (Exception me)
            {
                //throw new Exception("监听异常！异常信息：" + me.Message);
                Common.ErrorRecord.WriteErrorLog("监听异常！异常信息：" + me.Message);
                return;
            }  
  
            // Accept the next connection request  
            if (e.SocketError == SocketError.OperationAborted) return;  
            StartAccept(e);  
        }  
  
  
        void IO_Completed(object sender, SocketAsyncEventArgs e)  
        {  
            // determine which type of operation just completed and call the associated handler  
            switch (e.LastOperation)  
            {  
                case SocketAsyncOperation.Receive:  
                    ProcessReceive(e);  
                    break;  
                case SocketAsyncOperation.Send:  
                    ProcessSend(e);  
                    break;  
                default:  
                    throw new ArgumentException("The last operation completed on the socket was not a receive or send");  
            }  
  
        }  
  
  
        // This method is invoked when an asynchronous receive operation completes.   
        // If the remote host closed the connection, then the socket is closed.    
        // If data was received then the data is echoed back to the client.  
        //  
        private void ProcessReceive(SocketAsyncEventArgs e)  
        {
            try
            {  
                // check if the remote host closed the connection  
                AsyncUserToken token = (AsyncUserToken)e.UserToken;  
                if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)  
                {  
                    //读取数据  
                    byte[] data = new byte[e.BytesTransferred]; //e.BytesTransferred套接字缓冲字节数
                    token.BufferCount = e.BytesTransferred; //缓冲数量
                    //if (e.Buffer.Count() - e.Offset >= e.BytesTransferred)
                    Array.Copy(e.Buffer, e.Offset, data, 0, e.BytesTransferred);
                    //lock (token.Buffer)
                    //{
                    //    token.Buffer.AddRange(data);
                    //}
                    ////将数据包交给后台处理,这里你也可以新开个线程来处理.加快速度.  
                    if (ReceiveClientData != null)
                        ReceiveClientData(token, data);  
                    ////注意:你一定会问,这里为什么要用do-while循环?   
                    ////如果当客户发送大数据流的时候,e.BytesTransferred的大小就会比客户端发送过来的要小,  
                    ////需要分多次接收.所以收到包的时候,先判断包头的大小.够一个完整的包再处理.  
                    ////如果客户短时间内发送多个小数据包时, 服务器可能会一次性把他们全收了.  
                    ////这样如果没有一个循环来控制,那么只会处理第一个包,  
                    ////剩下的包全部留在token.Buffer中了,只有等下一个数据包过来后,才会放出一个来.  
                    //do
                    //{
                    //    //判断包的长度  
                    //  //  byte[] lenBytes = token.Buffer.GetRange(0, e.BytesTransferred).ToArray();
                    //    //int packageLen = BitConverter.ToInt32(lenBytes, 0);
                    //   // int packageLen = lenBytes.Count();
                    //    //if (packageLen >= token.Buffer.Count - e.BytesTransferred)
                    //    //{   //长度不够时,退出循环,让程序继续接收  
                    //    //    break;
                    //    //}
                    //    if (token.Buffer.Count < 16) break;
                    //    //包够长时,则提取出来,交给后面的程序去处理  
                    //    byte[] rev = token.Buffer.GetRange(0, 16).ToArray();
                    //    //从数据池中移除这组数据  
                    //    lock (token.Buffer)
                    //    {
                    //        token.Buffer.RemoveRange(0, 16);
                    //    }
                    //    //将数据包交给后台处理,这里你也可以新开个线程来处理.加快速度.  
                    //    if (ReceiveClientData != null)
                    //        ReceiveClientData(token, rev);
                    //    //这里API处理完后,并没有返回结果,当然结果是要返回的,却不是在这里, 这里的代码只管接收.  
                    //    //若要返回结果,可在API处理中调用此类对象的SendMessage方法,统一打包发送.不要被微软的示例给迷惑了.  
                    //} while (token.Buffer.Count >= 16);  
  
                    //继续接收. 为什么要这么写,请看Socket.ReceiveAsync方法的说明  
                    if (!token.Socket.ReceiveAsync(e))  
                        this.ProcessReceive(e);  
                }  
                else  
                {  
                    CloseClientSocket(e);  
                }
            }
            catch (Exception xe)
            {
                //throw new Exception(xe.Message);
                Common.ErrorRecord.WriteErrorLog(xe.Message);
            }  
        }  
  
        // This method is invoked when an asynchronous send operation completes.    
        // The method issues another receive on the socket to read any additional   
        // data sent from the client  
        //  
        // <param name="e"></param>  
        private void ProcessSend(SocketAsyncEventArgs e)  
        {  
            if (e.SocketError == SocketError.Success)  
            {  
                // done echoing data back to the client  
                AsyncUserToken token = (AsyncUserToken)e.UserToken;  
                // read the next block of data send from the client  
                bool willRaiseEvent = token.Socket.ReceiveAsync(e);  
                if (!willRaiseEvent)  
                {  
                    ProcessReceive(e);  
                }  
            }  
            else  
            {  
                CloseClientSocket(e);  
            }  
        }  
  
        //关闭客户端  
        private void CloseClientSocket(SocketAsyncEventArgs e)  
        {  
            AsyncUserToken token = e.UserToken as AsyncUserToken;  
  
            lock (m_clients) { m_clients.Remove(token); }  
            //如果有事件,则调用事件,发送客户端数量变化通知  
            if (ClientNumberChange != null)  
                ClientNumberChange(-1, token);  
            // close the socket associated with the client  
            //try  
            //{  
                //token.Socket.Shutdown(SocketShutdown.Send);  
            //}
            //catch (Exception ex) { throw new Exception(ex.Message); }  
            token.Socket.Close();  
            // decrement the counter keeping track of the total number of clients connected to the server  
            Interlocked.Decrement(ref m_clientCount);  
            m_maxNumberAcceptedClients.Release();  
            // Free the SocketAsyncEventArg so they can be reused by another client  
            e.UserToken = new AsyncUserToken();  
            m_pool.Push(e);  
        }  
  
  
  
        /// <summary>  
        /// 对数据进行打包,然后再发送  
        /// </summary>  
        /// <param name="token"></param>  
        /// <param name="message"></param>  
        /// <returns></returns>  
        public void SendMessage(AsyncUserToken token, byte[] message)  
        {  
            if (token == null || token.Socket == null || !token.Socket.Connected)  
                return;  
            try  
            {  
                //对要发送的消息,制定简单协议,头4字节指定包的大小,方便客户端接收(协议可以自己定)  
                //byte[] buff = new byte[message.Length];  
                //byte[] len = BitConverter.GetBytes(message.Length);  
                //Array.Copy(len, buff, 4);  
                //Array.Copy(message, 0, buff, 4, message.Length);  
                //token.Socket.Send(buff);  //这句也可以发送, 可根据自己的需要来选择  
                //新建异步发送对象, 发送消息  
                SocketAsyncEventArgs sendArg = new SocketAsyncEventArgs();  
                sendArg.UserToken = token;
                sendArg.SetBuffer(message, 0, message.Length);  //将数据放置进去.  
                token.Socket.SendAsync(sendArg);
                Thread.Sleep(50);
                token.Socket.SendAsync(sendArg);
            }  
            catch (Exception e){
                throw new Exception(e.Message);
            }  
        }
        /// <summary>
        /// Socket接收数据
        /// </summary>
        /// <returns></returns>
        public byte[] SocketReceiveData(Socket objSocket)
        {
            //定义byte数组存放从客户端接收过来的数据
            byte[] buffer = new byte[16];
            SocketAsyncEventArgs sendArg = new SocketAsyncEventArgs();
            sendArg.UserToken = objSocket;
            //将接收过来的数据放到buffer中，并返回实际接受数据的长度
            objSocket.Receive(buffer);
            return buffer;
        }
        public void SocketSendData(Socket objSocket, string data)
        {
            try
            {
                if (objSocket != null)
                {
                    byte[] buffer = new SerialPortHelper.AlgorithmHelper().From16ToBtyes(data);
                    SocketAsyncEventArgs sendArg = new SocketAsyncEventArgs();
                    sendArg.UserToken = objSocket;
                    sendArg.SetBuffer(buffer, 0, buffer.Length);  //将数据放置进去.  
                    objSocket.SendAsync(sendArg);
                    Thread.Sleep(50);
                    objSocket.SendAsync(sendArg);
                }
                else throw new Exception("服务端没有打开");
            }
            catch (Exception ex)
            {

                throw new Exception("发送失败，该IP客户端已断开连接；错误信息：" + ex.Message);
            }
        }
    }
}
